package com.zcf.service;

import com.zcf.common.json.Body;
import com.zcf.gameController.F_Websocket_s;
import com.zcf.gameController.WebSocketBull;
import com.zcf.gameController.gameUtils.gameLogic;
import com.zcf.mapper.RoomParamMapper;
import com.zcf.pojo.Game_club;
import com.zcf.pojo.RoomsParam;
import com.zcf.pojo.UserBean;
import com.zcf.vo.out.LayuiJson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZhaoQi
 * @since 2019-03-18
 */
@Service
public class RoomParamServiceImpl {
	@Autowired
	RoomParamMapper rp;

	/**
	 * 获取房间信息
	 * @return
	 */
	public LayuiJson getroominfo(RoomsParam room) {
		LayuiJson lj = new LayuiJson();
		List<Object> list = new ArrayList<>();
		Map<String, RoomsParam> rooms = WebSocketBull.rooms;
		Map<String, Object> roominfo =new HashMap<>() ;
		for (RoomsParam roomInfo : rooms.values()) {
			if(roomInfo.getRoomName() .equals(room.getRoomName())){
				for (UserBean user : roomInfo.getUsers().values()) {
					if(user.getType() == 1 || user.getType() == 2){
						roominfo =new HashMap<>() ;
						roominfo.put("userid", user.getId());
						roominfo.put("userName", user.getWxname());
						roominfo.put("userimg", user.getWximg());
						//roominfo.put("diamond", user.getDiamond());
						roominfo.put("sex", user.getSex());
						list.add(roominfo);
					}
				}
			}
		}
		
		
		if(list != null){
			lj.setCode(0);
			lj.setData(list);
			lj.setMsg("成功");
			return lj;
		}
		lj.setCode(1);
		lj.setData(list);
		lj.setMsg("暂无数据");
		return lj;
	}
	/**
	 * 后台获取房间列表
	 * @param param 
	 * @return
	 */
	public LayuiJson getrooms(RoomsParam param) {
		LayuiJson lj = new LayuiJson();
		List<Object> list = new ArrayList<>();
		Map<String, Object> roominfo ;
		Map<String, RoomsParam> rooms = WebSocketBull.rooms;
		for (RoomsParam room : rooms.values()) {
			roominfo = new HashMap<>();
			if(param.getRoomName() != null && param.getRoomName() != ""){
				if(room.getRoomName().equals(param.getRoomName())){
					list.clear();
					List<Object> list2 = getoneroom(room,list,roominfo);
					if(list2 != null){
						lj.setCode(0);
						lj.setCount(list2.size());
						lj.setData(list2);
						lj.setMsg("成功");
						return lj;
					}
				}
			}
			list = getoneroom(room, list, roominfo);
		}
		Map<String, RoomsParam> frooms = F_Websocket_s.rooms;
		for (RoomsParam room : frooms.values()) {
			roominfo = new HashMap<>();
			if(param.getRoomName() != null && param.getRoomName() != ""){
				if(room.getRoomName().equals(param.getRoomName())){
					list.clear();
					List<Object> list2 = getoneroom(room,list,roominfo);
					if(list2 != null){
						lj.setCode(0);
						lj.setCount(list2.size());
						lj.setData(list2);
						lj.setMsg("成功");
						return lj;
					}
				}
			}
			list = getoneroom(room, list, roominfo);
		}
		
		/*//Map<String, Game_club> clubrooms = WebSocketBull.Rooms;
		for (Game_club club : clubrooms.values()) {
			for (RoomsParam room : club.getClubs().values()) {
				roominfo = new HashMap<>();
				if(param.getRoomName() != null  && param.getRoomName() != ""){
					if( room.getRoomName().equals(param.getRoomName())){
						list.clear();
						List<Object> list2 = getoneroom(room,list,roominfo);
						if(list2 != null){
							lj.setCode(0);
							lj.setCount(list2.size());
							lj.setData(list2);
							lj.setMsg("成功");
							return lj;
						}
					}else{
						continue;
					}
				}
					
				list = getoneroom(room, list, roominfo);
			}
		}*/
		
		if(list != null){
			lj.setCode(0);
			//			lj.setCount(count);
			lj.setData(list);
			lj.setMsg("成功");
			return lj;
		}
		lj.setCode(1);
		//		lj.setCount(count);
		lj.setData(list);
		lj.setMsg("暂无数据");
		return lj;
	}
	/**
	 * 获取房间信息
	 * @param room
	 * @param list
	 * @param roominfo
	 * @return
	 */
	private List<Object> getoneroom(RoomsParam room, List<Object> list, Map<String, Object> roominfo){
		int num = 0;
		for (UserBean user : room.getUsers().values()) {
			if(user.getType() == 1 || user.getType() == 2){
				num ++;
			}
		}
		roominfo.put("roomName", room.getRoomName());
		roominfo.put("personnum",num+"/"+room.getRoomType().get("personnum"));
		roominfo.put("points", room.getRoomType().get("points"));
		roominfo.put("num", room.getRoomType().get("num"));
		roominfo.put("pushnote", room.getRoomType().get("pushnote"));
		roominfo.put("specialcard", room.getSpecialcard());
		roominfo.put("hoptions", room.getHoptions());
		roominfo.put("double", room.getRoomType().get("double"));
		roominfo.put("isStart", room.getIsStart());
		roominfo.put("gametype", room.getRoomType().get("roomType"));
		roominfo.put("gamerule", room.getGametype());
		list.add(roominfo);
		return list;
		
	}
	
	/**
	 * 已开房间
	 * @return
	 */
	public Body getopenroom() {
		Map<String, Object> map ;
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		if(WebSocketBull.rooms.size()!=0){
			for (RoomsParam room : WebSocketBull.rooms.values()) {
				map = new HashMap<String, Object>();
				map.put("gameType", room.getRoomType().get("gameType"));
				map.put("roomName", room.getRoomName());
				map.put("points", room.getRoomType().get("points"));
				map.put("num", room.getRoomType().get("num"));
				map.put("personnum", gameLogic.selectRoomUserTypes(room).size()+"/"+room.getRoomType().get("personnum"));
				list.add(map);
			}
			
		}
		return Body.newInstance(list);
	}
	/**
	 * 获取扎金花已开房间
	 * @return
	 */
	public Body getZopenroom() {
		Map<String, Object> map ;
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		if(F_Websocket_s.rooms.size()!=0){
			for (RoomsParam room : F_Websocket_s.rooms.values()) {
				map = new HashMap<String, Object>();
				map.put("gameType", room.getRoomType().get("gameType"));
				map.put("roomName", room.getRoomName());
				map.put("points", room.getRoomType().get("points"));
				map.put("num", room.getRoomType().get("num"));
				map.put("personnum", gameLogic.selectRoomUserTypes(room).size()+"/"+room.getRoomType().get("personnum"));
				list.add(map);
			}
			
		}
		return Body.newInstance(list);
	}
	

}
